/****************************************************
 *  Copyright © 2023-2030 CaiTao. All rights reserved.
 *------------------------------------------------------------------------
 *  文件：StructurePrincipleTemplate.cs
 *  作者：CT
 *  日期：Created by Fxb on 2023/4/17 8:54:28
 *  项目：混合动力汽车动力系统结构原理展示台
 *  功能：Nothing
*****************************************************/

using System.Collections.Generic;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.UI;
using Doozy.Engine;
using System;
using DG.Tweening;
//using UnityEngine.AddressableAssets;
using UnityEngine.Video;
using UnityEngine.Playables;
using System.Linq;
using System.Collections;
using Object = UnityEngine.Object;
using UnityEngine.UnityVideoPlayer;

namespace UnityEngine.UnityVideoPlayer
{
    //原理展示形式
    [Flags]
    public enum DisplayPattern
    { 
        Animation = 98,  //动画展示
        VideoPlay = 99,   //视频展示
        Structure =100  //结构展示
    }
	public partial class StructurePrincipleTemplate : AbstractRTTTemplate
    {
        private AudioSource audioSource;

        //改造
        public async  Task Load(RectTransform contentContainer, RectTransform exUiContainer, string customDatas)
        {
            audioSource = UnityEngine.Object.FindObjectOfType<AudioSource>();
            
            await Load_VideoPlay(contentContainer, exUiContainer, customDatas);
        }


        public override void FullScreenInBehind(RectTransform contentContainer, RectTransform fsExUIContainer)
        {
            base.FullScreenInBehind(contentContainer, fsExUIContainer);

            SetupRT(renderTarget);

            SetupViewPort(viewPortCache);
        }
    }
    

    /// <summary>
    /// 视频播放模板逻辑
    /// </summary>
    public partial class StructurePrincipleTemplate
    {
        #region Video Play
        protected VideoPlayerView videoPlayerView;
        protected UnityVideoPlayer videoPlayer;
        protected RenderTexture videoRTCache;

        protected List<(string title, string videoName)> videoBtns = new List<(string title, string videoName)>();
        protected List<VideoClip> clips = new List<VideoClip>();
        

        protected Transform videoControlButton;
        #endregion

        public async Task RefDatas_VideoPlay(string customDatas)
        {
            clips.Clear();

            await ExtractDatas();
        }

        public void PreUnload_VideoPlay()
        {
            if (videoPlayer != null)
            {
                videoPlayer.Clear();

                videoPlayer.OnVideoPrepared -= VideoPlayer_OnVideoPrepared;

                videoPlayer.OnFirstFrameReady -= VideoPlayer_OnFirstFrameReady;
            }
        }

        public void PostUnload_VideoPlay(AbstractTemplate newTempate)
        {
            videoPlayerView.Clear();

            videoPlayer.TargetTexture = null;

            if (videoRTCache != null)
                RenderTexture.ReleaseTemporary(videoRTCache);
            videoBtns.Clear();

            videoPlayer = null;
        }

        public async Task Load_VideoPlay(RectTransform contentContainer, RectTransform exUiContainer, string customDatas)
        {
            videoPlayer = UnityEngine.Object.FindObjectOfType<UnityVideoPlayer>();

            videoPlayer.Stop();

            videoPlayer.OnVideoPrepared -= VideoPlayer_OnVideoPrepared;

            videoPlayer.OnVideoPrepared += VideoPlayer_OnVideoPrepared;

            videoPlayer.OnFirstFrameReady -= VideoPlayer_OnFirstFrameReady;

            videoPlayer.OnFirstFrameReady += VideoPlayer_OnFirstFrameReady;

            //将上一个视频rt赋值进来防止闪烁
            if (videoPlayerView != null && videoPlayer.TargetTexture != null)
                videoPlayerView.Setup(videoPlayer, false);

            videoPlayer.IsLooping = true;

            if (videoPlayerView == null)
            {
                videoPlayerView = videoPlayer.GetComponent<VideoPlayerView>();
                
                videoPlayerView.OnFullScreenSwitch += VideoPlayerView_OnFullScreenSwitch;
                videoPlayerView.OnPlayStateEvent += VideoPlayerView_OnPlayStateEvent;
                //videoPlayerView.enabled = true;
            }

           

            videoControlButton.gameObject.SetActive(false);
            await RefDatas_VideoPlay(customDatas);
        }

        private void VideoPlayControl(int state)
        {
            if (state == -1)
            {
                videoPlayer.Stop();
                
            }
            else
            {
                videoPlayer.Play(clips[state]);
            }
        }

        protected async Task ExtractDatas()
        {
            //for (int i = 0; i < videoBtns.Count; i++)
            //{
            //    VideoClip videoClip = await Addressables.LoadAssetAsync<VideoClip>($"Movies/{videoBtns[i].videoName}").Task;
            //    clips.Add(videoClip);
            //}
        }

        private void VideoPlayer_OnFirstFrameReady()
        {
            videoPlayer.OnFirstFrameReady -= VideoPlayer_OnFirstFrameReady;

            videoPlayerView.Setup(videoPlayer, false);
        }

        private void VideoPlayer_OnVideoPrepared()
        {
            if (videoRTCache == null)
            {
                Debug.Assert(videoPlayer.Texture.width == 1920 && videoPlayer.Texture.height == 1080);

                videoRTCache = RenderTexture.GetTemporary(videoPlayer.Texture.width, videoPlayer.Texture.height);
            }

            videoPlayer.TargetTexture = videoRTCache;

            videoPlayerView.imgView.Tex = videoPlayer.TargetTexture;
        }

        private void VideoPlayerView_OnFullScreenSwitch(int fullModel)
        {
            //Message.Send(new CtrTemplateFullScreenMessage() { fsModel = fullModel });

            //if (fullScreenPopup != null)
            //    return;

            //fullScreenPopup = UIPopupManager.ShowPopup(DoozyNamesDB.POPUP_NAME_VIDEOPLAY, false, false).GetComponent<VideoPlayPopup>();

            //fullScreenPopup.videoPlayer = videoPlayer;
        }

        private void VideoPlayerView_OnPlayStateEvent(bool state)
        {
        }

        private void StopVideo()
        {
            VideoPlayControl(-1);
        }
    }
}

